// File:    CStorageNoteMgr.cs
// Author:  甘孝俭
// email:   ganxiaojian@hotmail.com 
// QQ:      154986287
// http://www.8088net.com
// 协议声明：本软件为开源系统，遵循国际开源组织协议。任何单位或个人可以使用或修改本软件源码，
//          可以用于作为非商业或商业用途，但由于使用本源码所引起的一切后果与作者无关。
//          未经作者许可，禁止任何企业或个人直接出售本源码或者把本软件作为独立的功能进行销售活动，
//          作者将保留追究责任的权利。
// Created: 2013/3/1 10:56:13
// Purpose: Definition of Class CStorageNoteMgr

using System;
using System.Text;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using System.Linq;
using ErpCoreModel.Framework;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Shared;


namespace ErpCoreModel.Invoicing
{
    public class CStorageNoteMgr : CBaseObjectMgr
    {

        public CStorageNoteMgr()
        {
            TbCode = "KC_StorageNote";
            ClassName = "ErpCoreModel.Invoicing.CStorageNote";
            Assembly assembly = Assembly.LoadFrom(Assembly.GetExecutingAssembly().Location);
            ObjType = assembly.GetType(ClassName);
        }

        public CStorageNote FindByCode(string sCode)
        {
            if (!Table.IsLowChange)
            {
                //大数据量 先考虑缓存
                foreach (CBaseObject obj in m_lstObj)
                {
                    CStorageNote StorageNote = (CStorageNote)obj;
                    if (StorageNote.Code.Equals(sCode, StringComparison.OrdinalIgnoreCase))
                        return StorageNote;
                }
                IMongoDatabase mongodb = Ctx.MainDB.GetMongoDB();

                BsonDocument doc = null;
                Task.Run(async () =>
                {
                    var collection = mongodb.GetCollection<BsonDocument>(TbCode);
                    var builder = Builders<BsonDocument>.Filter;
                    var filter = m_Filter;
                    if (filter == null)
                        filter = builder.Eq("code", sCode);
                    else
                        filter = filter & builder.Eq("code", sCode);
                    var cursor = await collection.FindAsync<BsonDocument>(filter);
                    if (!await cursor.MoveNextAsync())
                        return;
                    foreach (var document in cursor.Current)
                    {
                        doc = document;
                        break;
                    }
                }).Wait();

                if (doc == null)
                    return null;
                CStorageNote obj0 = new CStorageNote();
                obj0.Ctx = Ctx;
                obj0.Table = Table;
                obj0.m_ObjectMgr = (CBaseObjectMgr)this;

                obj0.Deserialize(doc);

                obj0.Commit();
                return obj0;
            }
            else
            {
                List<CBaseObject> lstObj = GetList();
                var varObj = from obj in lstObj where (obj as CStorageNote).Code == sCode select obj;
                if (varObj.Count() > 0)
                    return varObj.First() as CStorageNote;
                else
                    return null;
            }
        }
    }
}